#include "widget.h"
#include "ui_widget.h"
#include <QThread>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , m_pworker(new Worker)
{
    ui->setupUi(this);

    connect(ui->pushButton_caculate, &QPushButton::clicked, this, &Widget::caculatebtn_clicked);
    connect(this, &Widget::worker_caculate, m_pworker, &Worker::do_caculate);
    connect(m_pworker, &Worker::caculate_over, this, &Widget::caculate_rst);

    QThread* pthread = new QThread;
    m_pworker->moveToThread(pthread);
    pthread->start();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::caculatebtn_clicked(){
    QString x = ui->lineEdit_x->text();
    QString y = ui->lineEdit_y->text();

    emit worker_caculate(x.toInt(), y.toInt());

    return;
}

void Widget::caculate_rst(int rst){
    qDebug() << "GUI thread get the result...";

    ui->lineEdit_result->setText(QString::number(rst));

    return;
}

